GetRMSEfloat Function

private function GetRMSEfloat(grid1, grid2, maskReal, maskInteger, nrmse) result(rmse)

compute Root Mean Square Error between two grids real optional argument: mask: compute RMSE only on assigned mask nrmse: compute Normalizes Root Mean Square Error

Arguments

Type IntentOptional Attributes Name
type(grid_real), intent(in) :: grid1
type(grid_real), intent(in) :: grid2
type(grid_real), intent(in), optional :: maskReal
type(grid_integer), intent(in), optional :: maskInteger
logical, intent(in), optional :: nrmse

Return Value real(kind=float)


Variables

Type Visibility Attributes Name Initial
integer, public :: countCells
integer(kind=long), public :: i
integer(kind=long), public :: j
real(kind=float), public :: mean

Source Code

FUNCTION GetRMSEfloat &
!
(grid1, grid2, maskReal, maskInteger, nrmse) &
!
RESULT (rmse)

IMPLICIT NONE

!Arguments with intent(in):
TYPE (grid_real), INTENT(IN) :: grid1
TYPE (grid_real), INTENT(IN) :: grid2
TYPE (grid_real), OPTIONAL,  INTENT(IN) :: maskReal
TYPE (grid_integer), OPTIONAL,  INTENT(IN) :: maskInteger
LOGICAL, OPTIONAL, INTENT(IN) :: nrmse


!Local declarations:
INTEGER (KIND = long) :: i, j
REAL (KIND = float) :: rmse, mean
INTEGER             :: countCells
!---------------------------end of declarations--------------------------------

rmse = 0.
countCells = 0

!check grid1 and grid2 have the same coordinate reference system
 IF ( .NOT. CRSisEqual(grid1,grid2) ) THEN
      CALL Catch ('error', 'GridStatistics',  &
      'calculate RMSE: ', argument = &
      'coordinate reference system of grid1 differs from grid2' )
END IF

IF (PRESENT (maskReal)) THEN
    IF ( .NOT. CRSisEqual(maskReal,grid1) ) THEN
        CALL Catch ('error', 'GridStatistics',  &
        'calculate RMSE: ', argument = &
        'coordinate reference system of mask differs from input grid' )
    END IF
    
    !get mean
    !mean = GetMean (grid, maskReal = maskReal)

    DO j = 1, maskReal % jdim
        DO i = 1, maskReal % idim
            IF (maskReal % mat(i,j) /= maskReal % nodata) THEN
                rmse = rmse + ( grid1 % mat (i,j) - grid2 % mat (i,j)) **2.
                countCells = countCells + 1.
            END IF
        END DO
    END DO
    
ELSE IF (PRESENT (maskInteger)) THEN
    IF ( .NOT. CRSisEqual(maskInteger,grid1) ) THEN
        CALL Catch ('error', 'GridStatistics',  &
        'calculate RMSE: ', argument = &
        'coordinate reference system of mask differs from input grid' )
    END IF
  
    DO j = 1, maskInteger % jdim
        DO i = 1, maskInteger % idim
            IF (maskInteger % mat(i,j) /= maskInteger % nodata) THEN
                 rmse = rmse + ( grid1 % mat (i,j) - grid2 % mat (i,j)) **2.
                countCells = countCells + 1.
            END IF
        END DO
    END DO

ELSE
    
    DO j = 1, grid1 % jdim
        DO i = 1, grid1 % idim
            IF (grid1 % mat(i,j) /= grid1 % nodata) THEN
                rmse = rmse + ( grid1 % mat (i,j) - grid2 % mat (i,j)) **2.
                countCells = countCells + 1.
            END IF
        END DO
    END DO
END IF

rmse = ( rmse / countCells ) ** 0.5

IF (PRESENT(nrmse)) THEN
  IF (nrmse) THEN
    mean = GetMean (grid1)
    rmse = rmse / mean
  END IF
END IF

RETURN

END FUNCTION GetRMSEfloat